﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace DemoGame1
{
    public class Player : Character
    {
        IState playerState = new Normal();
        public bool lockcontrol = false;
        public My2DSprite finalbackground;
        public Ball ball;
        public CharacterStatusBar characterStatusBar;
        private Texture2D[] skillSequence;
        private bool skilling = false;
        private int mana = 100;

        public int Mana
        {
            get { return mana; }
            set { mana = value; }
        }

        public int getHP()
        {
            return HP;
        }
        public string charactername;
        public Player(ContentManager _content, string Charactername, Vector2 _topleft, Vector2 _size)
        {
            content = _content;
            charactername = Charactername;
            HP = 1000;
            if (Charactername == "Ryu")
            {
                standSequence = CreateTexture("Ryu\\stand\\Ryu_", 7);
                //   standSequence = CreateTexture("Boss\\skill\\ball\\Boss_",8);

                walkSequence = CreateTexture("Ryu\\walk\\Ryu_", 7);
                attackSequence = CreateTexture("Ryu\\attack\\Ryu_", 17);
                skillSequence = CreateTexture("Ryu\\skill\\Ryu_", 17);
                damagedSequence = CreateTexture("Ryu\\damaged\\Ryu_", 4);
                deadSequence = CreateTexture("Ryu\\dead\\Ryu_", 7);
                ball = new Ball(_content, "Ryu\\skill\\ball\\Ryu_", 17, _topleft, new Vector2(124, 63));
                ball.SetOrigin(new Vector2(302, 356));
                ball.Visible = false;
                InitPlayer(_topleft, _size);
                SetOrigin(new Vector2(290, 282));
                SetTransform(SpriteEffects.FlipHorizontally);

                ball.SetOrigin(new Vector2(302, 356));
                ball.Visible = false;
                InitPlayer(_topleft, _size);
                SetOrigin(new Vector2(290, 282));
                SetTransform(SpriteEffects.FlipHorizontally);

                Texture2D[] t = new Texture2D[1];
                t[0] = content.Load<Texture2D>("ryufinal");
                finalbackground = new My2DSprite(t, Vector2.Zero);

            }
            if (Charactername == "Ken")
            {
                standSequence = CreateTexture("KenMaster\\stand\\Ken_", 7);
                walkSequence = CreateTexture("KenMaster\\walk\\Ken_", 6);
                attackSequence = CreateTexture("KenMaster\\attack\\Ken_", 126);
                //attackSequence = CreateTexture("KenMaster\\attack\\Ken_", 50);
                skillSequence = CreateTexture("KenMaster\\kenskill\\Ken_", 17);

                damagedSequence = CreateTexture("KenMaster\\damaged\\Ken_", 4);
                deadSequence = CreateTexture("KenMaster\\dead\\Ken_", 8);
                ball = new Ball(_content, "KenMaster\\kenskill\\ball\\Ken_", 17, _topleft, new Vector2(62, 63));
                // ballSequence = CreateTexture("Ryu\\skill\\ball\\Ryu_", 16);
                ball.SetOrigin(new Vector2(302, 356));
                ball.Visible = false;
                InitPlayer(_topleft, _size);
                SetOrigin(new Vector2(290, 282));
                SetTransform(SpriteEffects.FlipHorizontally);
                Texture2D[] t = new Texture2D[1];
                t[0] = content.Load<Texture2D>("kenfinal");
                finalbackground = new My2DSprite(t, Vector2.Zero);
            }
            characterStatusBar = new CharacterStatusBar(content, Charactername);
            // this.SetScale(1.5f);
        }

        private void InitPlayer(Vector2 _topleft, Vector2 _size)
        {
            TopLeft = _topleft;
            Size = _size;
            Sprites = new List<My2DSprite>();
            My2DSprite character = new My2DSprite(standSequence, TopLeft);
            Sprites.Add(character);
            nSprites = Sprites.Count;
        }
        public override void SetFrameSequence(int type)
        {
            if (type == SKILL)
            {
                lockcontrol = true;
                Sprites[0].Textures = skillSequence;

            }
            base.SetFrameSequence(type);
        }

        public override void Update(GameTime gameTime)
        {

            SPEED = playerState.getspeed();
            BoundRect = new Rectangle((int)TopLeft.X, (int)TopLeft.Y, (int)_Size.X, (int)_Size.Y);
            this.SetScale(1.5f);
            // if (lastDirection == RIGHT)
            if (Sprites[0].SpriteEffects == SpriteEffects.FlipHorizontally)
            {
                if (Status == ATK)
                {
                    BoundRect = new Rectangle((int)TopLeft.X + (int)_Size.X + 30, (int)TopLeft.Y, 30, 80);

                }
                if (Status == SKILL && skilling == false)
                {
                    skilling = true;
                    ball.TopLeft = new Vector2(TopLeft.X + _Size.X + 35, TopLeft.Y);
                    if (charactername == "Ken")
                    {
                        ball.SetOrigin(new Vector2(352, 356));
                    }
                    ball.SetTransform(SpriteEffects.FlipHorizontally);
                    ball.adjustTopleft(ball.TopLeft.X + 70, ball.TopLeft.Y);

                    ball.Range = 20.0f;
                }

            }
            ///if (lastDirection == LEFT)
            if (Sprites[0].SpriteEffects == SpriteEffects.None)
            {
                if (Status == ATK)
                {
                    BoundRect = new Rectangle((int)TopLeft.X - 30, (int)TopLeft.Y, 30, 80);
                }
                if (Status == SKILL && skilling == false)
                {
                    //      Mana -= 85;
                    skilling = true;
                    //     BoundRect = new Rectangle((int)(TopLeft.X -ball.Size.X-75), (int)TopLeft.Y, 20, 20);
                    //   sprites[1].Position = new Vector2(rect.X+65, rect.Y-75);
                    ball.TopLeft = new Vector2((TopLeft.X - ball.Size.X - 75), TopLeft.Y);
                    if (charactername == "Ken")
                    {
                        ball.SetOrigin(new Vector2(302, 356));
                    }
                    ball.SetTransform(SpriteEffects.None);
                    ball.adjustTopleft(ball.TopLeft.X + 70, ball.TopLeft.Y);
                    ball.Range = -20.0f;
                }
            }
            if (Status == SKILL)
            {
                if (Sprites[0].iTextures == 9)
                {
                    ball.Visible = true;
                    ball.Collide = false;
                }
                if (Sprites[0].iTextures + 1 == Sprites[0].Textures.Length)
                {
                    SetFrameSequence(STAND);
                    Status = STAND;

                }
            }
            if (Status == DAMAGED)
            {
                if (Sprites[0].iTextures + 1 == Sprites[0].Textures.Length)
                {
                    //
                    myGame.mediaControler.PlaySoundEffect(MediaControler.PLAYER.damaged);
                    //
                }
            }
            if (Status == STAND)
            {
                if (Mana < 100)
                    Mana += 1;
            }
            if (ball.Sprites[0].iTextures + 1 == ball.Sprites[0].Textures.Length)
            {
                lockcontrol = false;
                ball.Sprites[0].iTextures = 0;
                SetFrameSequence(STAND);
                skilling = false;
                ball.Visible = false;
            }

            if (dead > 0)
            {
                dead++;
                if (Sprites[0].iTextures + 1 == Sprites[0].nTextures)
                {
                    bUpdate = false;
                    // Visible = false;

                }
                if (dead == 20)
                {
                    Visible = false;
                }
                if (dead < 10)
                {
                    myGame.mediaControler.PlaySoundEffect(MediaControler.PLAYER.dead);
                }
            }

            ball.Update(gameTime);
            base.Update(gameTime);
            characterStatusBar.Health = getHP();
            characterStatusBar.Mana = Mana;
            characterStatusBar.Update(gameTime);
            this.playerState.Update(this);

        }
        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {

            base.Draw(spriteBatch, gameTime);
            ball.Draw(spriteBatch, gameTime);
            //  characterStatusBar.Draw(sb, gameTime);
        }

        private void Damaged(int damage)
        {
            if (dead == 0)
            {
                ball.Visible = false;
                if (Status != DAMAGED)
                {

                    Status = DAMAGED;
                    SetFrameSequence(DAMAGED);

                }

                lockcontrol = false;
                HP = HP - damage;

                if (HP < 0)
                {
                    dead = 1;
                    lockcontrol = true;
                    if (Status != DEAD)
                    {
                        Status = DEAD;
                        SetFrameSequence(DEAD);
                    }
                    HP = 0;
                    return;
                }
            }

        }
        public override int OnEvent(Global.EVENTS eventID, object sender, object arg)
        {
            switch (eventID)
            {
                case Global.EVENTS.GM_PLAYER_DAMAGED:
                    this.Damaged((int)arg);
                    break;
            }
            return 0;

        }
        public Boolean CollideWithMap(Rectangle[] rects)
        {
            for (int i = 0; i < rects.Length; i++)
            {
                /* if (lastDirection == UP)
                 {
                     Rectangle t = this.BoundRect;
                     t.Y +=(int) this.Size.Y;
                     if (t.Intersects(rects[i]))
                         return true;
                 }
                 else
                 {
                     if (this.BoundRect.Intersects(rects[i]))
                         return true;
                 }*/
                BoundRect = new Rectangle((int)TopLeft.X, (int)TopLeft.Y, (int)_Size.X, (int)_Size.Y);
                this.SetScale(1.5f);
                Rectangle t = this.BoundRect;
                t.Y += 135;
                t.Height -= 135;


                if (t.Intersects(rects[i]))
                    return true;

            }
            return false;
        }

        public void setState(IState iState)
        {
            this.playerState = iState;

        }
    }
}
